﻿#pragma once
#include "FCMiddleware_global.h"
#include "Depacker.h"
#include "TcpClient.h"
#include <QThread>
#include <QMutex>

namespace FCMiddleware
{
    class FCMIDDLEWARE_EXPORT TcpThread : public QThread
	{
		Q_OBJECT

	public:
        TcpThread(Depacker* depacker, const QString& hostName, unsigned short hostPort, QObject *parent = nullptr);
		~TcpThread();

	protected:
		QString _hostName;
		unsigned short _hostPort;
		bool _connected;

		QMutex _runLocker;
        volatile bool _isRunning;
        volatile bool _running;

        TcpClient* _tcpClient;
		Depacker* _depacker;

	protected:
		void run() override;
        virtual bool sendPacket();
        virtual bool recvPacket();

	public:
		void setHostName(const QString& hostName) { _hostName = hostName; }
		void setHostPort(unsigned short hostPort) { _hostPort = hostPort; }
		void setHost(const QString& hostName, unsigned short hostPort);
		void host(QString& hostName, unsigned short& hostPort);

		void startServer();
		void stopServer();
		bool isConnected() const { return _connected; }

	signals:
        void stateChanged(unsigned int state, int err, int extra = 0);
        void ackReady(void* frame);
	};
}

